# -*- coding: utf8 -*-
#Autor: Iván Viguera
#Programa: manipulaDates.py
#Descripció:
#########
import datetime

def changeDate(dateAux):
    try:
        y = int(raw_input('Any -yyyy-: '))
        m = int(raw_input('Mes -mm-: '))
        d = int(raw_input('Día -dd-: '))
        dateAux = dateAux.replace(year=y,month=m,day=d)
    except ValueError: pass
    return dateAux

def increaseDay(dateAux):
    try:
        return dateAux.replace(day=dateAux.day+1)
    except ValueError:
        return dateAux.replace(day=1)
    
def decreaseDay(dateAux):
    try:
        dateAux = dateAux.replace(day=dateAux.day-1)
    except ValueError:
        if dateAux.month == 2:
            if dateAux.year%4 == 0 and (dateAux.year%100!=0 or dateAux.year%400==0):
                dateAux = dateAux.replace(day=29)
            else:
                dateAux = dateAux.replace(day=28)                
        elif (dateAux.month % 2 == 0 and dateAux.month<7) or (dateAux.month % 2 <> 0 and dateAux.month>8):
            dateAux = dateAux.replace(day=30)
        else:
            dateAux = dateAux.replace(day=31)
    return dateAux

def increaseMonth(dateAux):
    try:
        return dateAux.replace(month=dateAux.month+1)
    except ValueError:
        return dateAux.replace(month=1)
    
def decreaseMonth(dateAux):
    try:
        return dateAux.replace(month=dateAux.month-1)
    except ValueError:
        if dateAux.month-1 == 2:
            if dateAux.year%4 == 0 and (dateAux.year%100!=0 or dateAux.year%400==0):
                dateAux = dateAux.replace(day=29)
            else:
                dateAux = dateAux.replace(day=28)                
        elif dateAux.month-1 % 2 == 0 and dateAux.month-1<>8:
            dateAux = dateAux.replace(day=30)
        else:
            dateAux = dateAux.replace(day=31)
        return dateAux.replace(month=dateAux.month-1)

def increaseYear(dateAux):
    try:
        return dateAux.replace(year=dateAux.year+1)
    except ValueError:
        return dateAux.replace(year=dateAux.year+1,day=28)

def decreaseYear(dateAux):
    try:
        return dateAux.replace(year=dateAux.year-1)
    except ValueError:
        return dateAux.replace(year=dateAux.year-1,day=28)
    
def sortir(null):
    return None
fecha = datetime.date.today()

opcionsMenu = ('a. Canvia la data',
               'b. Incrementa un día',
               'c. Decrementa un día',
               'd. Incrementa un mes',
               'e. Decrementa un mes',
               'f. Incrementa un any',
               'g. Decrementa un any',
               'x. Surt.')
functions = dict({'a':changeDate,
                  'b':increaseDay,
                  'c':decreaseDay,
                  'd':increaseMonth,
                  'e':decreaseMonth,
                  'f':increaseYear,
                  'g':decreaseYear,
                  'x':sortir})
while fecha <> None:
    print fecha 
    for op in opcionsMenu:
        print op
    try: fecha = functions.get(raw_input('Opció: '))(fecha)
    except Exception, e: print e
